<?php
namespace Back\Controller;
use Back\Common\BackController;

class GoodsController extends BackController{
    public function showlist(){
        $model = D("Goods");
        $info = $model -> fetchData();
        $this -> assign("show",$info["show"]);
        $this -> assign("list",$info["list"]);
        $this -> display();
    }

    public function add(){
        $data = D("Goods");
        if(IS_POST){
            if($_FILES["goods_big_logo"]["error"] === 4){
                $this -> error("请上传商品大图",U("add"));
            }
            $info = $data -> create();
            $info["goods_introduce"] = fangXSS($_POST["goods_introduce"]);
            $bool = $data -> add($info);
            if($bool){
                $this -> success("添加商品成功",U("showlist"));
            }else{
                $this -> error("添加商品失败",U("add"));
            }
        }else{
            $this -> display();
        }
    }

    public function update(){
        $model = D("Goods");
        if(IS_POST){
            $goods_id = I("get.goods_id");
            $data = $model -> create();
            $data["goods_introduce"] = fangXSS($_POST["goods_introduce"]);
            $bool = $model ->where("goods_id = $goods_id") -> save($data);
            if($bool){
                $this -> success("修改商品成功",U("showlist"));
            }else{
                $this -> error("修改商品失败");
            }
        }else{
            $goods_id = I("get.goods_id");
            $info = $model -> find($goods_id);
            $picsinfo = D("Pics") -> where("goods_id = $goods_id") -> select();
            $this -> assign("picsinfo",$picsinfo);
            $this -> assign("info",$info);
            $this -> display();
        }
    }

    public function delpics(){
        $id = I("get.id");
        $model = D("Pics");
        $info = $model -> find($id);
        unlink($info["pics_big"]);
        unlink($info["pics_small"]);

        if($model -> delete($id)){
            echo "删除成功";
        }
    }

    public function delgoods(){
        $goods_id = I("get.goods_id");
        $result = D("Goods") -> where("goods_id = $goods_id") -> setField("is_del","删除");
        if($result){
            echo json_encode(array("status"=>1));
        }else{
            echo json_encode(array("status"=>0));
        }
    }
}